package com.loe.ktui.custom

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.core.content.res.getBooleanOrThrow
import androidx.core.content.res.getDimensionOrThrow
import androidx.core.content.res.getStringOrThrow
import com.google.android.flexbox.*
import com.loe.ktui.*
import com.loe.ktui.util.KtCornerProvider
import com.loe.ktui.util.UiShape

open class UiFlexLayout(id: String? = null) : UiGroup(id)
{
    var flexDirection: Int? = null
    var divider: Any? = null
    var dividerV: Any? = null
    var showDividerV: Int? = null
    var dividerH: Any? = null
    var showDividerH: Int? = null
    var maxLine: Int? = null
    var alignItems: Int? = null
    var alignContent: Int? = null
    var justifyContent: Int? = null
    var flexWrap: Int? = FlexWrap.WRAP

    open fun initUI(context: Context, view: KtFlexLayout)
    {
        super.initUI(context, view)

        isAlphaClick?.let { view.isAlphaClick = it }

        flexDirection?.let { view.flexDirection = it }
        divider?.let { view.setDividerV(it, showDividerV); view.setDividerH(it, showDividerH) }
        dividerV?.let { view.setDividerV(it, showDividerV) }
        dividerH?.let { view.setDividerH(it, showDividerH) }
        maxLine?.let { view.maxLine = it }
        alignItems?.let { view.alignItems = it }
        alignContent?.let { view.alignContent = it }
        justifyContent?.let { view.justifyContent = it }
        flexWrap?.let { view.flexWrap = it }

        // 递归子元素
        childs.forEach()
        {
            it.createView(context, view)
        }
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtFlexLayout
    {
        return KtFlexLayout(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_FlexLayout(id: String? = null, block: (UiFlexLayout.() -> Unit)? = null): UiFlexLayout
{
    val ui = UiFlexLayout(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

open class KtFlexLayout : FlexboxLayout
{
    fun initUI(id: String? = null, block: (UiFlexLayout.() -> Unit)? = null)
    {
        removeAllViews()

        val ui = UiFlexLayout(id)
        block?.let { ui.it() }
        ui.initUI(context, this)
    }

    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(context.obtainStyledAttributes(attrs, com.loe.ktui.R.styleable.KtFlexLayout))
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(context.obtainStyledAttributes(attrs, com.loe.ktui.R.styleable.KtFlexLayout, defStyleAttr, 0))
    }

    private fun initXML(typedArray: TypedArray? = null)
    {
        typedArray?.let()
        {
            if (it.hasValue(com.loe.ktui.R.styleable.KtFlexLayout_kt_corner))
            {
                val corner = it.getDimensionOrThrow(com.loe.ktui.R.styleable.KtFlexLayout_kt_corner).toInt()
                val cornerDirect =  KtDirects[it.getInt(com.loe.ktui.R.styleable.KtFlexLayout_kt_cornerDirect, 4)]

                outlineProvider = KtCornerProvider(corner, cornerDirect, this)
            }
            if (it.hasValue(com.loe.ktui.R.styleable.KtFlexLayout_kt_isAlphaClick))
            {
                isAlphaClick = it.getBooleanOrThrow(com.loe.ktui.R.styleable.KtFlexLayout_kt_isAlphaClick)
            }
            if (it.hasValue(com.loe.ktui.R.styleable.KtButton_kt_link))
            {
                link = it.getStringOrThrow(com.loe.ktui.R.styleable.KtButton_kt_link)
            }

            it.recycle()
        }
    }

    /********************************************************************************************/

    fun setDividerV(divider: Any, showDividerV: Int? = null)
    {
        if (divider is UiShape)
        {
            dividerDrawableHorizontal = divider.createDrawable()
        } else
        {
            divider.toString().preLoadCallback(context) { drawable -> dividerDrawableHorizontal = drawable }
        }
        // showDividerV
        showDividerHorizontal = showDividerV ?: SHOW_DIVIDER_MIDDLE
    }

    fun setDividerH(divider: Any, showDividerH: Int? = null)
    {
        if (divider is UiShape)
        {
            dividerDrawableVertical = divider.createDrawable()
        } else
        {
            divider.toString().preLoadCallback(context) { drawable -> dividerDrawableVertical = drawable }
        }
        // showDividerH
        showDividerVertical = showDividerH ?: SHOW_DIVIDER_MIDDLE
    }

    /********************************************** 点击 ****************************************/

    var link = ""
        set(value)
        {
            field = value
            setKtDelayClickListener { KtUi.onLink?.invoke(context, value) }
        }

    var isAlphaClick = false
        set(value)
        {
            field = value
            isClickable = value
        }

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlphaClick) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlphaClick) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }
}